const { Sequelize } = require('sequelize');
const config = require('./config');

// 创建Sequelize实例
const sequelize = new Sequelize(
  config.database.name,
  config.database.user,
  config.database.password,
  {
    host: config.database.host,
    port: config.database.port,
    dialect: config.database.dialect,
    pool: config.database.pool,
    logging: config.database.logging && console.log,
    define: {
      // 使用下划线命名法
      underscored: true,
      // 禁止自动复数表名
      freezeTableName: true
    }
  }
);

// 测试数据库连接
const testConnection = async () => {
  try {
    await sequelize.authenticate();
  } catch (error) {
    console.error(`数据库连接测试失败:`, error);
    console.error(`数据库配置:`, {
      host: config.database.host,
      port: config.database.port,
      database: config.database.name,
      user: config.database.user,
      dialect: config.database.dialect
    });
  }
};

module.exports = {
  sequelize,
  testConnection
}; 